﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.DiemDanhSinhVienService;
namespace eTMS.Controllers
{
    public class DiemDanhSinhVienController : DiemDanhSinhVienControllerBase
    {
        public DiemDanhSinhVienController()
            : base()
        {

        }
        public DataTable GetLyDoNghi(bool isAddChoose,string chooseText)
        {
            DataTable tbl = new DataTable();
            tbl.Columns.Add("ID", typeof(Byte));
            tbl.Columns.Add("Name", typeof(string));
            DataRow row;
            if (isAddChoose)
            {
                row = tbl.NewRow();
                row["ID"] = 0;
                row["Name"] = chooseText;
                tbl.Rows.Add(row);
            }
            row = tbl.NewRow();
            row["ID"] = 1;
            row["Name"] = "Không phép";
            tbl.Rows.Add(row);
            row = tbl.NewRow();
            row["ID"] = 2;
            row["Name"] = "Có phép";
            tbl.Rows.Add(row);
            return tbl;
        }
        private DataTable _Table;
        private DiemDanhSinhVienEntity oDiemDanhSinhVien;
        public DiemDanhSinhVienEntity DiemDanhSinhVienObject
        {
            get
            {
                return oDiemDanhSinhVien;
            }
            set
            {
                oDiemDanhSinhVien = value;
            }
        }
        public DataTable Table
        {
            get
            {
                return _Table;
            }
            set
            {
                _Table = value;
            }
        }
        public void LoadAllDSDiemDanhSinhVien(int NamHoc_ID, int HocKy_ID, DateTime? TuNgay, DateTime? DenNgay)
        {
            DataSet ds = dao.SelectAllDSWithLopMonHoc(HocKy_ID, NamHoc_ID, TuNgay, DenNgay);
            _Table = ds.Tables[0];
        }
        public void LoadAllDSSinhVienNghiHoc(int HocKy_ID, int NamHoc_ID, int MonHoc_ID,string Filter)
        {
            DiemDanhSinhVienService.DiemDanhSinhVienServiceClient wDiemDanh = new eTMS.Controllers.DiemDanhSinhVienService.DiemDanhSinhVienServiceClient();
            _Table = wDiemDanh.SelectAllDSSinhVienSoLanNghiHoc(HocKy_ID, NamHoc_ID, MonHoc_ID, Filter).Tables[0];
        }
        public List<int> GetListSinhVien_Id()
        {
            List<int> lsID = new List<int>();
            foreach (DataRow r in _Table.Rows)
            {
                lsID.Add(ConvertInt(r["SinhVien_Id"]));
            }
            return lsID;
        }
        public void AddSinhVienInfoToTable(DataTable tblSinhVienInfo)
        {
            tblSinhVienInfo.Columns.Add("SoTietNghi",typeof(int));
            foreach (DataRow r in tblSinhVienInfo.Rows)
            {
                DataRow[] rs = Table.Select("SinhVien_ID="+r["SinhVien_Id"].ToString());
                if (rs.Length > 0)
                {
                    r["SoTietNghi"] = rs[0]["SoLanNghi"];
                }
            }
            _Table = tblSinhVienInfo;
        }
        public void AddIndexToTable()
        {
            this.AddIndexToTable(_Table);
        }
        public void ResetTableIndex()
        {
            this.ResetTableIndex(_Table);
        }
        public void LoadObject(Int32 DiemDanhSinhVien_ID)
        {
            oDiemDanhSinhVien = dao.SelectOne(DiemDanhSinhVien_ID);
        }
        public bool AddNew()
        {
            if (oDiemDanhSinhVien == null) return false;
            bool result = true;
            try
            {
                oDiemDanhSinhVien = dao.SaveRet(oDiemDanhSinhVien);
                result &= oDiemDanhSinhVien.DiemDanhSinhVien_ID > 0;
            }
            catch
            {
                result = false;
            }

            return result;
        }
        public void SaveDetails(Dictionary<int, byte> dicDetails)
        {
            SinhVienNghiHocService.SinhVienNghiHocServiceClient wDetails = new eTMS.Controllers.SinhVienNghiHocService.SinhVienNghiHocServiceClient();
            foreach (int id in dicDetails.Keys)
            {
                SinhVienNghiHocService.SinhVienNghiHocEntity o = new eTMS.Controllers.SinhVienNghiHocService.SinhVienNghiHocEntity();
                o.SinhVien_ID = id;
                o.DiemDanhSinhVien_ID = DiemDanhSinhVienObject.DiemDanhSinhVien_ID;
                o.LoaiNghi = dicDetails[id];
                wDetails.Save(o);
            }
        }
        public bool Update()
        {
            if (oDiemDanhSinhVien == null) return false;
            return dao.Update(oDiemDanhSinhVien);
        }
        public bool Delete()
        {
            if (oDiemDanhSinhVien == null) return false;
            bool retvalue = dao.Delete(oDiemDanhSinhVien);

            return retvalue;
        }
        public bool DeleteByID(Int32 DiemDanhSinhVien_ID)
        {
            bool retvalue = dao.DeleteByID(DiemDanhSinhVien_ID);

            return retvalue;
        }
        public void ResetObject()
        {
            this.oDiemDanhSinhVien = new DiemDanhSinhVienEntity();
        }
        public void MapObjectToDataRow(DataRow row)
        {
            this.MapObjectToDataRow(this.DiemDanhSinhVienObject, row);
        }
        public DiemDanhSinhVienEntity LoadDiemDanhSinhVien(Int32 DiemDanhSinhVien_ID)
        {
            DiemDanhSinhVienEntity o = dao.SelectOne(DiemDanhSinhVien_ID);
            return o;
        }
        private DataTable _TableSinhVien;
        public DataTable TableSinhVien
        {
            get
            {
                return _TableSinhVien;
            }
        }
        public void LoadSinhVienNghiHoc(int DiemDanhSinhVien_ID)
        {
            SinhVienNghiHocService.SinhVienNghiHocServiceClient wDetails = new eTMS.Controllers.SinhVienNghiHocService.SinhVienNghiHocServiceClient();
            _TableSinhVien = wDetails.SelectAllDSSinhVienNghiHoc(DiemDanhSinhVien_ID).Tables[0];
            SinhVienController.AddTenGioiTinh(_TableSinhVien);
            SortByLastName("TenSinhVien", "HoSV", "TenSV", _TableSinhVien);
            AddIndexToTable(_TableSinhVien);
            _TableSinhVien.AcceptChanges();
        }
        public void DeleteDiemDanhSinhVien(List<DataRow> lsRows)
        {
            SinhVienNghiHocService.SinhVienNghiHocServiceClient wDetails = new eTMS.Controllers.SinhVienNghiHocService.SinhVienNghiHocServiceClient();
            foreach (DataRow r in lsRows)
            {
                wDetails.DeleteByID(ConvertInt(r["SinhVienNghiHoc_ID"]));
                _TableSinhVien.Rows.Remove(r);
            }
            _TableSinhVien.AcceptChanges();
        }
        public bool UpdateLyDo()
        {
            bool result = true;
            SinhVienNghiHocService.SinhVienNghiHocServiceClient wDetails = new eTMS.Controllers.SinhVienNghiHocService.SinhVienNghiHocServiceClient();
            foreach (DataRow row in _TableSinhVien.Rows)
            {
                if (row.RowState == DataRowState.Modified)
                {
                    result &= wDetails.UpdateLoaiNghi(ConvertByte(row["LoaiNghi"]), ConvertInt(row["SinhVienNghiHoc_ID"]));
                }
            }
            _TableSinhVien.AcceptChanges();
            return result;
        }
    }
}
